package server;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;

import database.bean.WebcamBEAN;
import database.dao.WebcamDAO;

public class FileManager implements Runnable {
	private WebcamDAO	wbc		= null;
	private String		path	= "/var/lib/tomcat6/webapps/videos/";

	public FileManager( WebcamDAO wbc ) {
		this.wbc = wbc;
	}

	@Override
	public void run() {
		while( true ) {
			try {
				ArrayList<WebcamBEAN> list = wbc.getAllVideos();
				for( WebcamBEAN webcam : list ) {
					File file = new File( path + webcam.getVideoName() );
					if( file.lastModified() < getTime() ) {
						file.delete();
						wbc.deleteVideo( webcam.getVideoName() );
					}
				}
				Thread.sleep( 3600000 );// sleep for 60 minuts
			} catch( InterruptedException e ) {
				e.printStackTrace();
			}
		}
	}

	@SuppressWarnings( "static-access" )
	private long getTime() {
		Calendar cal = Calendar.getInstance();
		cal.add( cal.DATE, -3 );
		return cal.getTimeInMillis();
	}

}
